import config from '@/config.js';
import api_sign_secret from "./api_sign.js";
import utils from "./utils.js";
export function request(url, param, isShowLoading = "1", method = 'POST') {
	if (isShowLoading) {
		uni.showLoading({
			title: '正在加载...'
		})
	}
	console.log("config",config);
	console.log("param before",param);
	param=api_sign_secret(param);
	console.log("param after",param);
	let header = {};
	header['X-Token'] =utils.getStorage("token");// uni.getStorageSync('adminId');
	// header['Current-School-Id'] = uni.getStorageSync('schoolId');
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.host + url,
			data: param,
			method: method,
			header: header,
			success(res) {
				//console.log(res);
				if (res.data.code === 0) { //找不到openid对应的用户
					resolve(res.data);
				} else if (res.data.code == 101) {
					console.log("res.code","未登录",res);
					//未登录
					let redirectToRes= uni.redirectTo({
						url:"/pages/user/add",
						success:function(){
							
							reject(res.data);
						},
						complete:function(e){
							console.log("complete",e);
						}
					});
					console.log("uni",uni.redirectTo,redirectToRes);
					
				} else {
					reject(res.data);
				}
			},
			fail(err) {
				reject(err);
			},
			complete() {
				if(isShowLoading){
					uni.hideLoading();
				}
				
			}
		});
	});
}
